/*
 *
 *    Copyright (c) 2023 Project CHIP Authors
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package chip.devicecontroller.cluster.structs

import chip.devicecontroller.cluster.*
import matter.tlv.ContextSpecificTag
import matter.tlv.Tag
import matter.tlv.TlvReader
import matter.tlv.TlvWriter

class CameraAvStreamManagementClusterRateDistortionTradeOffPointsStruct(
  val codec: UInt,
  val resolution: CameraAvStreamManagementClusterVideoResolutionStruct,
  val minBitRate: ULong,
) {
  override fun toString(): String = buildString {
    append("CameraAvStreamManagementClusterRateDistortionTradeOffPointsStruct {\n")
    append("\tcodec : $codec\n")
    append("\tresolution : $resolution\n")
    append("\tminBitRate : $minBitRate\n")
    append("}\n")
  }

  fun toTlv(tlvTag: Tag, tlvWriter: TlvWriter) {
    tlvWriter.apply {
      startStructure(tlvTag)
      put(ContextSpecificTag(TAG_CODEC), codec)
      resolution.toTlv(ContextSpecificTag(TAG_RESOLUTION), this)
      put(ContextSpecificTag(TAG_MIN_BIT_RATE), minBitRate)
      endStructure()
    }
  }

  companion object {
    private const val TAG_CODEC = 0
    private const val TAG_RESOLUTION = 1
    private const val TAG_MIN_BIT_RATE = 2

    fun fromTlv(
      tlvTag: Tag,
      tlvReader: TlvReader,
    ): CameraAvStreamManagementClusterRateDistortionTradeOffPointsStruct {
      tlvReader.enterStructure(tlvTag)
      val codec = tlvReader.getUInt(ContextSpecificTag(TAG_CODEC))
      val resolution =
        CameraAvStreamManagementClusterVideoResolutionStruct.fromTlv(
          ContextSpecificTag(TAG_RESOLUTION),
          tlvReader,
        )
      val minBitRate = tlvReader.getULong(ContextSpecificTag(TAG_MIN_BIT_RATE))

      tlvReader.exitContainer()

      return CameraAvStreamManagementClusterRateDistortionTradeOffPointsStruct(
        codec,
        resolution,
        minBitRate,
      )
    }
  }
}
